from apps.common.base.dao import BaseDao
from apps.user.serializers import *
from django.db.models import F
from apps.system.rank.dao import RankDao
import datetime
from decimal import Decimal
from apps.common.utils.tools import random_str, random_uuid
from apps.common.extension.customException import *

class UserDao(BaseDao):
    MODEL_CLASS = UserModel
    SERI_CLASS = UserBaseSerializer

    def findAllUser(self):
        _list = UserDao().findAll().filter(role__key='custom').select_related('parent', 'role')
        return _list
    
    def findAllAdmin(self):
        _list = UserDao().findAll().exclude(role__key='custom').select_related('parent', 'role')
        return _list
    
    def createUser(self, data):
        _user = self.create(data)
        if _user:
            code = 100000 + _user.id
            _user = self.update(_user.id, { "code": code, "username": code })
        return _user
        
    def replaceToken(self, userId):

        old_token = Token.objects.filter(user_id=userId)
        old_token.delete()
        token = Token.objects.create(user_id=userId)

        return token